#include "main_listener.h"


using namespace Ogre;

MainListener::MainListener(Ogre::Root *root, OIS::Keyboard *keyboard, OIS::Mouse *mouse) : mKeyboard(keyboard),mMouse(mouse), mRoot(root) 
{
	mProfessorNode = mRoot->getSceneManager("main")->getSceneNode("Professor");

	mNinjaNode = mRoot->getSceneManager("main")->getSceneNode("Ninja");
	mCamera = mRoot->getSceneManager("main")->getCamera("main");

	mProfessorNode->yaw(Degree(270));
}

bool MainListener::frameStarted(const FrameEvent &evt)
{
	
    mKeyboard->capture();

	static float angle = 0;
	static float cameraPitch = 0;

	float dt = evt.timeSinceLastFrame;
	mMouse->capture();
	if(mKeyboard->isKeyDown(OIS::KC_RIGHT)){
		mProfessorNode->yaw(-Radian(1.5f*dt));
		angle += 1.5f*dt;
	}
	if(mKeyboard->isKeyDown(OIS::KC_LEFT)){
		mProfessorNode->yaw(Radian(1.5f*dt));
		angle -= 1.5f*dt;
	}


	if(mKeyboard->isKeyDown(OIS::KC_W)){
		mProfessorNode->translate(-cosf(angle)*200*dt,0,-sinf(angle)*200*dt);
	}
	if(mKeyboard->isKeyDown(OIS::KC_S)){
		mProfessorNode->translate(cosf(angle)*200*dt,0,sinf(angle)*200*dt);
	}
	if(mKeyboard->isKeyDown(OIS::KC_A)){
		mProfessorNode->translate(-sinf(angle)*200*dt,0,cosf(angle)*200*dt);
	}
	if(mKeyboard->isKeyDown(OIS::KC_D)){
		mProfessorNode->translate(sinf(angle)*200*dt,0,-cosf(angle)*200*dt);
	}
	mProfessorNode->yaw(Degree(static_cast<Ogre::Real>(mMouse->getMouseState().X.rel)));
	angle += mMouse->getMouseState().X.rel;

	static float velocityN = 100;

	if(mNinjaNode->getPosition().x < -400 || mNinjaNode->getPosition().x > 400){
		velocityN *= -1;
	mNinjaNode->translate(velocityN * evt.timeSinceLastFrame, 0, 0);
	}
	mNinjaNode->translate(velocityN * evt.timeSinceLastFrame, 0, 0);

	//loat angle = atan2f(mProfessorNode->getPosition().z - mNinjaNode->getPosition().z,mProfessorNode->getPosition().x - mNinjaNode->getPosition().x);

	  
	mCamera->lookAt(mProfessorNode->getPosition().x,mProfessorNode->getPosition().y+200,mProfessorNode->getPosition().z);
	mCamera->setPosition(mProfessorNode->getPosition().x + cosf(angle)*300,mProfessorNode->getPosition().y+300,mProfessorNode->getPosition().z+sinf(angle)*300);
    
	// -----------------------------------------------------------------------------------------
	return true;
}

bool mouseMoved(const OIS::MouseEvent &event)
{
	  


	return true;
}